package com.h2psoft.truyen.tranh.activities;

import java.util.ArrayList;

import vn.persoft.extra.customview.CustomToast;
import vn.persoft.extra.dialog.ConfirmDialog;
import vn.persoft.extra.internet.Internet;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.h2psoft.truyen.tranh.R;
import com.h2psoft.truyen.tranh.adapter.AdapterListChap;
import com.h2psoft.truyen.tranh.constants.Constants;
import com.h2psoft.truyen.tranh.dialog.LoadingDialog;
import com.h2psoft.truyen.tranh.objects.ObjChaper;
import com.h2psoft.truyen.tranh.server.ServerHandling;

public class ActivityComicInfo extends Activity implements OnScrollListener, OnClickListener {
	private String tag = ActivityComicInfo.class.getName();

	private ImageView imgComicLogo;
	private TextView tvwComicName, tvwComicAuthor, tvwEditor, tvwStatic, tvwChapCount;
	private ListView lvlListChap;
	private AdapterListChap adapterListChap;
	private ArrayList<ObjChaper> arrChapers;
	private ArrayList<ObjChaper> temp;
	private ArrayList<String> arrPagesUrls;
	private int start = 0, mPrevTotalItemCount = -10, comicId, comicCount;
	private boolean loadingMore = false;
	private Context context;
	private LoadingDialog loadingDialog;
	private Intent intent;
	private String comicName, comicEditor, comicStatic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comic_info);
		context = this;
		getExtra();
		initView();
	}

	private void getExtra() {
		try {
			intent = getIntent();
			Bundle bundle = intent.getExtras();

			if (bundle != null) {
				comicId = bundle.getInt(Constants.KEY_COMIC_ID);
				comicName = bundle.getString(Constants.KEY_COMIC_NAME);
				comicEditor = bundle.getString(Constants.KEY_COMIC_EDITOR);
				comicStatic = bundle.getString(Constants.KEY_COMIC_STATUS);
				comicCount = bundle.getInt(Constants.KEY_COMIC_COUNT);
			}
		} catch (Exception e) {
			finish();
		}
	}

	private void initView() {
		imgComicLogo = (ImageView) findViewById(R.id.imgComicLogo_ComicInfo);
		tvwChapCount = (TextView) findViewById(R.id.tvwChapCount_ComicInfo);
		tvwComicAuthor = (TextView) findViewById(R.id.tvwConmicAuthor_ComicInfo);
		tvwComicName = (TextView) findViewById(R.id.tvwComicName_ComicInfo);
		tvwEditor = (TextView) findViewById(R.id.tvwComicEditor_ComicInfo);
		tvwStatic = (TextView) findViewById(R.id.tvwComicStatic_ComicInfo);
		lvlListChap = (ListView) findViewById(R.id.lvlListChap_ComicInfo);
		loadingDialog = new LoadingDialog(context);
		imgComicLogo.setOnClickListener(this);
		arrPagesUrls = new ArrayList<String>();
		arrPagesUrls.clear();
		tvwComicName.setText(comicName);
		tvwComicAuthor.setText(comicEditor);
		tvwStatic.setText(comicStatic);
		tvwChapCount.setText(" " + comicCount);
		arrChapers = new ArrayList<ObjChaper>();
		arrChapers.clear();
		adapterListChap = new AdapterListChap(context, arrChapers);
		lvlListChap.setAdapter(adapterListChap);
		lvlListChap.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				intent = new Intent(context, ActivityInComic.class);
				intent.putExtra(Constants.KEY_ARRAY_URL, arrPagesUrls);
				intent.putExtra(Constants.KEY_TARGET_PAGE, arg2);
				intent.putExtra(Constants.KEY_COMIC_ID, comicId);
				startActivity(intent);
			}
		});
		lvlListChap.setOnScrollListener(this);
	}

	// Copyright (C) 2011 - 2012 by PERSO STOCK COMPANY.
	private void loadListChaper() {
		loadingDialog.show();
		if (start == 0)
			lvlListChap.setVisibility(View.GONE);
		if (start % Constants.LOAD_COUNT == 0) {
			if (Internet.isConnectedInternet(context))
				new Thread(new Runnable() {
					public void run() {
						synchronized (this) {
							if (!loadingMore) {
								loadingMore = true;
								try {
									ServerHandling getListChap = new ServerHandling();
									temp = new ArrayList<ObjChaper>();
									temp.clear();
									getListChap.getListChaper(temp, comicId, start);
								} catch (Exception e) {
									Log.e(tag, e.toString());
								}
								runOnUiThread(new Runnable() {
									@Override
									public void run() {
										loadingMore = false;
										loadingDialog.dismiss();
										start += temp.size();
										arrChapers.addAll(temp);
										int tempSize = temp.size();
										for (int i = 0; i < tempSize; i++) {
											arrPagesUrls.add(temp.get(i).getImgUrl());
											arrPagesUrls.add(temp.get(i).getSecondImgUrl());
										}

										adapterListChap.notifyDataSetChanged();
										if (temp.size() != 0) {
											lvlListChap.setVisibility(View.VISIBLE);
											lvlListChap.requestLayout();
										}
									}
								});
							}
						}
					}
				}).start();
			else {
				loadingDialog.dismiss();
				CustomToast.showRandom(this, R.string.dont_connect_internet);
			}
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(context, MainActivity.class);
			startActivity(intent);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		int lastInScreen = firstVisibleItem + visibleItemCount;
		if ((lastInScreen == totalItemCount) && !(loadingMore) && totalItemCount != mPrevTotalItemCount) {
			mPrevTotalItemCount = totalItemCount;
			loadListChaper();
		}

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imgComicLogo_ComicInfo:
			Intent intent = new Intent(context, MainActivity.class);
			startActivity(intent);
			finish();
			break;
		}

	}
}
